/*
Copyright (C) 2013 Matthew Netsch

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "passtool.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QSize>
#include <QIcon>

PassTool::PassTool(QWidget *parent) :
    QToolBar(parent),
    passgen(NULL)
{
    initState();
    createActions();
}

void
PassTool::initState()
{
    setFloatable(false);
    setMovable(false);
    setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    this->setIconSize(QSize(32, 32));

    passgen = new PassGen();
}

void
PassTool::createActions()
{
    addAction(QIcon(":/icons/new.png"), "New");
    addAction(QIcon(":/icons/open.png"), "Open");
    addAction(QIcon(":/icons/save.png"), "Save");
    addAction(QIcon(":/icons/delete.png"), "Delete");
    addSeparator();
    addAction(QIcon(":/icons/addpass.png"), "Add Pass");
    addAction(QIcon(":/icons/generate.png"), "Generate");
    addAction(QIcon(":/icons/copy.png"), "Copy Pass");
    addAction(QIcon(":/icons/deletepass.png"), "Delete Pass");
    addSeparator();
    addAction(QIcon(":/icons/options.png"), "Options");
    addAction(QIcon(":/icons/about.png"), "About");

    connect(this, SIGNAL(actionTriggered(QAction*)), SLOT(actionHandler(QAction*)));
    connect(passgen, SIGNAL(passShowed(bool)), this, SLOT(showPass(bool)));
}

void
PassTool::showPass(
        bool show)
{
    emit passShowed(show);
}

bool
PassTool::getShowPass()
{
    return passgen->getShowPass();
}

void
PassTool::actionHandler(
        QAction *action)
{
    if (action->text() == "Add Pass")
    {
        bool ok;
        char *value = (char*)malloc(sizeof(char) * passgen->getSize());
        passgen->generate(value);

        QString name = QInputDialog::getText(this, tr("Add Password"), tr("Password name:"), QLineEdit::Normal, "", &ok);

        if (ok && !name.isEmpty())
        {
            emit passAdded(name, value);
        }

        free(value);
    }
    else if (action->text() == "New")
    {
        emit newDoc();
    }
    else if (action->text() == "Save")
    {
        emit saveDoc();
    }
    else if (action->text() == "Open")
    {
        emit openDoc();
    }
    else if (action->text() == "Delete")
    {
        emit deleteDoc();
    }
    else if (action->text() == "Options")
    {
        passgen->show();
    }
    else if (action->text() == "Delete Pass")
    {
        emit passDeleted();
    }
    else if (action->text() == "Copy Pass")
    {
        emit passCopied();
    }
    else if (action->text() == "Generate")
    {
        char *value = (char*)malloc(sizeof(char) * passgen->getSize());
        passgen->generate(value);

        emit passChanged(value);

        free(value);
    }
    else if (action->text() == "About")
    {
        QMessageBox::about(this, "About PassMan",
                           "PassMan 1.0\n\n" \
                           "27 May 2013\n\n" \
                           "Icons in Public Domain courtesy of the Tango Desktop Project\n" \
                           "http://tango.freedesktop.org\n\n" \
                           "License:\n" \
                           "Copyright (C) 2013 Matthew Netsch\n\n"\
                           "This library is free software; you can redistribute it and/or\n"\
                           "modify it under the terms of the GNU Lesser General Public\n"\
                           "License as published by the Free Software Foundation; either\n"\
                           "version 2.1 of the License, or (at your option) any later version.\n\n"\
                           "This library is distributed in the hope that it will be useful,\n"\
                           "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"\
                           "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"\
                           "Lesser General Public License for more details.\n\n"\
                           "You should have received a copy of the GNU Lesser General Public\n"\
                           "License along with this library; if not, write to the Free Software\n"\
                           "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\n");
    }
}
